package com.dennytech.tac.ui.activity;

import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dennytech.tac.base.CFActivity;
import com.dennytech.tac.model.Pair;
import com.dennytech.tac.model.TravelGuide;
import com.dennytech.tac.statistics.Stat;
import com.dennytech.tac.utils.CFSpannableString;
import com.dennytech.tac.widget.NetworkImageView;
import com.dennytech.tac.R;

public class TravelGuideInfoActivity extends CFActivity {

	private static final String LOG_TAG = TravelGuideInfoActivity.class
			.getSimpleName();

	private TravelGuide tg;

	private TextView titleTv;
	private TextView introTv;
	private NetworkImageView headImg;
	private TextView detailTv;
	private LinearLayout travelLay;

	@Override
	protected void onCreate(Bundle b) {
		super.onCreate(b);
	}

	@Override
	protected void initView() {
		setContentView(R.layout.travel_guide_info);

		titleTv = (TextView) findViewById(R.id.title);
		introTv = (TextView) findViewById(R.id.intro);
		headImg = (NetworkImageView) findViewById(R.id.head_img);
		headImg.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Stat.event("travelguideinfo", "travelguideinfo_img_click", null, 0);
			}
		});
		travelLay = (LinearLayout) findViewById(R.id.travel_lay);
		detailTv = (TextView) findViewById(R.id.detail_txt);
		detailTv.setMovementMethod(LinkMovementMethod.getInstance());

		tg = getIntent().getParcelableExtra("tg");
		if (tg != null) {
			titleTv.setText(tg.getName());
			introTv.setText(tg.getIntro());
			headImg.setImage(tg.getPhotoUrl());
			addTraves();
			detailTv.setText(new CFSpannableString(tg.getDetail()).string());
		} else {
			Log.w(LOG_TAG, "can't start this page because tg is null");
			finish();
		}
	}

	private void addTraves() {
		travelLay.removeAllViews();

		Pair[] pairs = tg.getTravel();
		for (int i = 0; i < pairs.length; i++) {
			if (pairs[i] == null) {
				continue;
			}

			View view = getLayoutInflater().inflate(R.layout.travel_item,
					travelLay, false);

			((TextView) view.findViewById(R.id.title)).setText((pairs[i]
					.getTitle()));
			TextView content = (TextView) view.findViewById(R.id.content);
			content.setText(new CFSpannableString(pairs[i].getContent())
					.string());
			content.setMovementMethod(LinkMovementMethod.getInstance());

			travelLay.addView(view);
		}
	}

}
